﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MDF.Worker.Threading.ThreadPools.TaskItems;
using MDF.Logger.Interfaces;
using MDF.Logger.Managements;


namespace MDF.Worker.Threading
{
    /// <summary>
    /// A sample task
    /// </summary>
    public abstract class ITask : ITaskItem
    {
        /// <summary>
        /// Logger
        /// </summary>
        private ILog m_log = LogManager.GetInstance();
        /// <summary>
        /// Callback when finished job
        /// </summary>
        /// <param name="sender">Owner class</param>
        /// <param name="result">The result of work</param>
        public delegate void onFinishedHandeler(object sender, object result);
        public delegate void onErrorHandeler(object sender, object result);
        public delegate void onChangeStatusHandeler(object sender, object result);
        /// <summary>
        /// Event finished
        /// </summary>
        public event onFinishedHandeler onFinished = null;
        public event onErrorHandeler onError = null;
        public event onChangeStatusHandeler onChangeStatus = null;
        /// <summary>
        /// Work's priority
        /// </summary>
        private TaskItemPriority _priority;

        public TaskItemPriority Priority
        {
            get { return _priority; }
            set { _priority = value; }
        }
        /// <summary>
        /// Work's index
        /// </summary>
        private int _taskIndex;

        protected int _iStatus = 0;

        private string m_taskName = "";

        public string TaskName
        {
            get { return m_taskName; }
            set { m_taskName = value; }
        }

        public int Status
        {
            get { return _iStatus; }
        }
        protected void WaitCaptcha()
        {
            _iStatus = 4;
        }
        /*
         * 0: Waiting
         * 1: Working
         * 2: Finished
         * 3: Error
         * 4: Waiting
         * */

        public int TaskIndex
        {
            get { return _taskIndex; }
            set { _taskIndex = value; }
        }

        public void DoWork()
        {
            m_log.Info(string.Format("Working task {0}", _taskIndex));
            this._iStatus = 1;
            if (onChangeStatus != null)
                this.onChangeStatus(this, _iStatus);
            object result = null;
            try
            {
                
                result = this.RunIt();
                if (_iStatus == 4)
                {
                    return;
                }
                this._iStatus = 2;
                if (onChangeStatus != null)
                    this.onChangeStatus(this, _iStatus);
                if (onFinished != null)
                {
                    m_log.Info(string.Format("Finished task {0}", _taskIndex));
                    
                    this.onFinished(this, result);
                    MDF.Worker.Threading.ThreadControl.m_lstTask.Remove(this);
                    m_log.Info(string.Format("Removed task {0}", _taskIndex));
                }
            }
            catch (Exception ex)
            {
                if (onError != null)
                {
                    m_log.Error(string.Format("Error task {0}", _taskIndex), ex);
                    this._iStatus = 3;
                    if (onChangeStatus != null)
                        this.onChangeStatus(this, _iStatus);
                    this.onError(this, ex);
                    MDF.Worker.Threading.ThreadControl.m_lstTask.Remove(this);
                    m_log.Info(string.Format("Removed task {0}", _taskIndex));
                }
            }
        }
        /// <summary>
        /// All work push in
        /// </summary>
        public virtual object RunIt()
        {
            return null;
        }
    }
}
